﻿using Kepp.Extend.Class;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading.Tasks;

namespace Kepp.Extend
{
    /// <summary>
    /// 公用方法类
    /// </summary>
    public class MyClass
    {
        public unpublics un = new unpublics();
        /// <summary>
        /// 输出函数
        /// </summary>
        /// <param name="funcName"></param>
        public void callFunc(string funcName)
        {
            MethodInfo method = GetType().GetMethod(funcName);
            Func<string> func = (Func<string>)Delegate.CreateDelegate(typeof(Func<string>), this, method);
            func();
        }
        /// <summary>
        /// 结束方法
        /// </summary>
        /// <returns></returns>
        public string res()
        {
            Console.WriteLine("\n结束\n");
            return null;
        }
        #region 基本控制练习
        /// <summary>
        /// 九九乘法表
        /// </summary>
        /// <returns></returns>
        public string fun1_1()
        {
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write($"{j} * {i} = {j * i}\t");
                }
                Console.Write("\n");
            }
            return this.res();
        }
        /// <summary>
        /// 菱形图案
        /// </summary>
        /// <returns></returns>
        public string fun1_2()
        {
            int i = 0;
            int x = 1;
            int y = 0;
            int e = 2;
            char a;
            while (true)
            {
                Console.WriteLine("请输入一个字符!");
                string ch = Console.ReadLine();
                if (ch.Length == 1)
                {
                    a = char.Parse(ch);
                    break;
                }
            }
            while (true)
            {
                Console.WriteLine("请输入宽度!(最大127)");
                string ch = Console.ReadLine();
                sbyte number = 0;
                if (sbyte.TryParse(ch, out number))
                {
                    if (!((i += number) % 2 == 1)) i++;
                    break;
                }
            }
            do
            {
                y = (i - x) / 2;
                Console.WriteLine($"{new string(' ', y)}{new string('@', x)}{new string('\u0000', y)}");
                x += e;
                if (x >= i) e = -2;
                if (x < 0) break;
            } while (true);


            return this.res();
        }
        #region 猜数字抽象函数
        /// <summary>
        /// 猜数字等小游戏
        /// </summary>
        /// <returns></returns>
        public string fun1_3()
        {
            new Student().SayHi();
            return this.res();
        }
        /// <summary>
        /// 抽象父类
        /// </summary>
        abstract class Person
        {
            public abstract Person SayHi();
            public abstract Person Standup(sbyte val);
            public abstract Person Shuru();
        }
        /// <summary>
        /// 抽象学生类
        /// </summary>
        class Student : Person
        {
            /// <summary>
            /// 随机数
            /// </summary>
            public sbyte number { get; set; }
            /// <summary>
            /// 初始化
            /// </summary>
            /// <returns></returns>
            public override Student SayHi()
            {
                this.number = (sbyte)new Random().Next(1, 100);
                Console.WriteLine("数字100以内猜大小");
                return this.Shuru();
            }

            public override Student Standup(sbyte val)
            {
                if (val > this.number)
                {
                    Console.WriteLine("太大了，重新试试");
                    return this.Shuru();
                }
                else if (val < this.number)
                {
                    Console.WriteLine("太小了，在加加油");
                    return this.Shuru();
                }
                Console.WriteLine($"恭喜你猜对了！！！中奖数字{this.number}");
                return this;
            }
            /// <summary>
            /// 输入数字
            /// </summary>
            /// <returns></returns>
            public override Student Shuru()
            {
                Console.WriteLine("请输入一个数字");
                string str = Console.ReadLine();
                sbyte val = 0;
                if (!sbyte.TryParse(str, out val))
                {
                    Console.WriteLine("输入不合法");
                    return this.Shuru();
                }
                return this.Standup(val);
            }
        }
        /// <summary>
        /// 抽象教师
        /// </summary>
        class Teacher : Person
        {
            public override Teacher SayHi()
            {
                return this;
            }

            public override Teacher Standup(sbyte val)
            {
                return this;
            }
            public override Teacher Shuru()
            {
                return this;
            }
        }
        #endregion
        /// <summary>
        /// 求质数
        /// </summary>
        /// <returns></returns>
        public string fun1_4()
        {
            Console.WriteLine("输入一个自然数求质数");
            int number = un.Number();
            int sum = 0;
            for (int i = 3; i <= number; i++)
            {
                bool flag = true;
                for (int j = 2; j <= (i / 2 + 1); j++)
                {
                    if (i % j != 0) continue;
                    flag = false;
                    break;
                }
                if (flag)
                {
                    sum++;
                    Console.WriteLine($"·{i}·是质数");
                }
            }
            Console.WriteLine("总共{0}个质数",sum);
            return this.res();
        }

        /// <summary>
        /// 有一列数1，1，2，3，5，........求第30个数
        /// </summary>
        /// <returns></returns>
        public string fun1_5()
        {
            Console.WriteLine("请输入计算次数");
            int number = un.Number();
            long a=1,b=0,c=0;
            Console.Write("{0}   ", a);
            for (int i = 2; i < number; i++)
            {
                c = a + b;
                b = a;
                a = c;
                Console.Write("{0}   ",c);
            }
            return res();
        }

        /// <summary>
        /// 小青蛙跳台阶
        /// </summary>
        /// <returns></returns>
        public string fun1_6()
        {
            Console.WriteLine("请输入...");
            Console.WriteLine(Duck(un.Number()) + "种方法"); 
            return res();
        }
        public int Duck(int n)
        {
            //f(0) = 0,f(1) = 1，等价于 n<=2时，f(n) = n。
            if (n <= 2)
            {
                return n;
            }
            return Duck(n-1) + Duck(n - 2);
        }

        public string fun1_7()
        {

            return res();
        }
        #endregion

        #region C#递归小练习
        public string fun2_1()
        {
            int count = 0;
            for (int i = 1; i <= 8; i++)
            {
                if (i == 8)
                {
                    Console.WriteLine("村民经过了7个村庄还剩下2只");
                    break;
                }
                count += Duck1(i);
                Console.WriteLine($"经过第{i}个村庄时，卖出 {Duck1(i)}只鸭子");
            }
            Console.WriteLine("总共卖出了{0}只", count);

            Console.WriteLine($"请问村民有{count}只鸭子,经过多少个村庄可以卖完");
            int number = 0;
            while (count > 1)
            {
                count = count / 2 - 1;
                Console.WriteLine($"村民进过第{++number}个村庄，卖了{count}只鸭子");
            }
            Console.WriteLine("村民总共进过了{0}个村庄，然后卖完了", number);
            return this.res();
        }

        public int Duck1(int a)
        {
            if (a == 8)//回归条件
            {
                return 2;
            }
            return 2 * (Duck1(a + 1) + 1); //传递
            //x/2-1=y      x= 2*(y+1)
        }

        public string fun2_2()
        {
            Console.WriteLine("输入一个自然数");
            Console.WriteLine($"统计:{Duck2()}次");
            return this.res();
        }
        public int Duck2()
        {
            int number = 0;
            if (int.TryParse(Console.ReadLine(), out number)) return Duck3(number,1);
            return Duck2();
        }
        public int Duck3(int number,int count)
        {
            if (number % 2 == 0)
                number /= 2;
            else
                number = number * 3 + 1;
            Console.WriteLine($"状态：第{count}次的值是：{number}");
            return number == 1?count:Duck3(number,++count);
        }
        #endregion

        #region C#基本算法练习
        /// <summary>
        /// 求和
        /// </summary>
        /// <returns></returns>
        public string fun3_1()
        {
            Console.WriteLine("输入两个自然数求和");
            int todo1 = un.Number();
            int todo2 = un.Number();
            Console.WriteLine("他们的结果是{0}", todo1 == todo2 ? (todo1 + todo2) * 3 : todo1 + todo2);
            return res();
        }
        /// <summary>
        /// 实现n和51的差的绝对值
        /// </summary>
        /// <returns></returns>
        public string fun3_2()
        {
            Console.WriteLine("请输入1个自然数");
            int todo1 = un.Number();
            todo1 = todo1 - 51 > 0 ? (todo1-51) * 3 : todo1 - 51;
            Console.WriteLine("结果为{0}", todo1);
            return res();
        }
        /// <summary>
        /// 来检查两个整数
        /// </summary>
        /// <returns></returns>
        public string fun3_3()
        {
            Console.WriteLine("输入两个自然数");
            int todo1 = un.Number();
            int todo2 = un.Number();
            Console.WriteLine("结果为{0}", (todo1==30 || todo2==30 || todo1 + todo2==30) ? "true":"false");
            return res();
        }
        /// <summary>
        /// 检验OK开头
        /// </summary>
        /// <returns></returns>
        public string fun3_4()
        {
            Console.WriteLine("输入一串字符");
            string todo1 = Console.ReadLine();
            if (todo1.StartsWith("OK"))
            {
                Console.WriteLine(todo1);
            }
            Console.WriteLine("OK{0}",todo1);
            return res();
        }
        /// <summary>
        /// 删除指定字符串位置
        /// </summary>
        /// <returns></returns>
        public string fun3_5()
        {
            Console.WriteLine("输入一串字符");
            string todo1 = Console.ReadLine();
            int todo2 = 0;
            Console.WriteLine("输入要删除的位置");
            while (true)
            {
                todo2 = un.Number();
                if (todo2 < todo1.Length)
                {
                    break;
                }
                Console.WriteLine("太长了位置不够");
            }
            Console.WriteLine("结果{0}", todo1.Remove(todo2, 1));
            return res();
        }
        #endregion
    }
}
